package popcorn.ticket.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import popcorn.ticket.entity.MovieTypes;
import popcorn.ticket.entity.MovieTypesExample;
import popcorn.ticket.service.MovieTypeService;
import popcorn.ticket.tool.AjaxResponse;
import popcorn.ticket.tool.PageData;

@Controller
public class MovieTypeController {
	
	@Autowired
	private MovieTypeService mts;
	
	
	Logger logger = Logger.getLogger(MovieTypeController.class);
	
	@RequestMapping("MovieType")
	@ResponseBody
	public PageData<MovieTypes> getMovieType(@RequestParam(name = "page",defaultValue = "1") int page,
			@RequestParam(name = "limit",defaultValue = "10") int limit,
			@RequestParam(name = "key",defaultValue = "") String key){
		logger.info("key:"+key);
		MovieTypesExample example = new MovieTypesExample();
		example.createCriteria().andMtypenameLike("%" + key + "%");
		return mts.selectByExample(example, page, limit);
	}
	
	@RequestMapping("AllType")
	@ResponseBody
	public List<MovieTypes> getAllType(){
		return mts.selectAllType();
	}
	
	String msg = "失败";
	int code = 0;
	
	@PostMapping("addMovieType")
	@ResponseBody
	public AjaxResponse addMovieType(@RequestBody MovieTypes mt) {

		boolean flag = mts.insert(mt) > 0;
		if(flag) {
			msg = "添加成功";
			code = 200;
			return ar(msg,code);
		}
		return ar(msg,code);	
	}
	
	@RequestMapping(value="delMovieType/{id}",method = RequestMethod.DELETE)
	@ResponseBody
	public AjaxResponse delMovieType(@PathVariable("id") int mtypeid) {
		
		boolean flag = mts.deleteByPrimaryKey(mtypeid) > 0;
		if(flag) {
			msg = "删除成功";
			code = 200;
			return ar(msg, code);
		}
		return ar(msg, code);
		
	}
	@PostMapping("updMovieType")
	@ResponseBody
	public AjaxResponse updMovieType(@RequestBody MovieTypes mt) {
		
		boolean flag = mts.updateByPrimaryKeySelective(mt) > 0;
		if(flag) {
			msg = "修改成功";
			code = 200;
			return ar(msg, code);
		}
		return ar(msg, code);
	}
	
	@RequestMapping(value = "delMoreType",method = RequestMethod.DELETE)
	@ResponseBody
	public AjaxResponse delMoreType(@RequestBody List<Integer> list) {

		MovieTypesExample example = new MovieTypesExample();
		example.createCriteria().andMtypeidIn(list);
		int result = mts.deleteByExample(example);
		msg = "成功删除"+result+"条";
		code = 200;
		return ar(msg, code);
	}
	
	
	public AjaxResponse ar(String msg,int code) {
		AjaxResponse ar = new AjaxResponse(msg, code);
		return ar;
	}
	
}
